package POJO;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;

public class RuleMesgJSON {
    @JsonProperty("event")
    private String event = "SelectCard";

    @JsonProperty("clazz")
    private String clazz = "Product";

    @JsonProperty("pos")
    private pos pos = new pos();
    @JsonProperty("nor")
    private nor nor = new nor();

    @JsonProperty("product")
    private Product product;

    @JsonProperty("parent")
    private Parent parent;

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public Parent getParent() {
        return parent;
    }

    public void setParent(Parent parent) {
        this.parent = parent;
    }
    @JsonProperty("userInput")
    private List<UserInput> userInput;


    public List<UserInput> getUserInput() {
        return userInput;
    }

    public void setUserInput(List<UserInput> userInput) {
        this.userInput = userInput;
    }



    @JsonProperty("right")
    private ProductSZ right = new ProductSZ();

    @JsonProperty("left")
    private ProductSZ left = new ProductSZ();

    @JsonProperty("front")
    private ProductSZ front = new ProductSZ();

    @JsonProperty("back")
    private ProductSZ back = new ProductSZ();

    @JsonProperty("up")
    private ProductSZ up = new ProductSZ();

    @JsonProperty("down")
    private ProductSZ down = new ProductSZ();

    public String getEvent() {
        return event;
    }

    public void setEvent(String event) {
        this.event = event;
    }

    public String getClazz() {
        return clazz;
    }

    public void setClazz(String clazz) {
        this.clazz = clazz;
    }

    public pos getPos() {
        return pos;
    }

    public void setPos(pos pos) {
        this.pos = pos;
    }

    public nor getNor() {
        return nor;
    }

    public void setNor(nor nor) {
        this.nor = nor;
    }

    public ProductSZ getRight() {
        return right;
    }

    public void setRight(ProductSZ right) {
        this.right = right;
    }

    public ProductSZ getLeft() {
        return left;
    }

    public void setLeft(ProductSZ left) {
        this.left = left;
    }

    public ProductSZ getFront() {
        return front;
    }

    public void setFront(ProductSZ front) {
        this.front = front;
    }

    public ProductSZ getBack() {
        return back;
    }

    public void setBack(ProductSZ back) {
        this.back = back;
    }

    public ProductSZ getUp() {
        return up;
    }

    public void setUp(ProductSZ up) {
        this.up = up;
    }

    public ProductSZ getDown() {
        return down;
    }

    public void setDown(ProductSZ down) {
        this.down = down;
    }


    /**
     * 将List集合导出为Excel
     * @param dataList 要导出的数据集合
     * @param filePath 保存路径（含文件名）
     * @param <T> 集合元素类型
     * @throws IOException IO异常
     * @throws IllegalAccessException 反射访问异常
     */
    public static <T> void exportToExcel(List<T> dataList, String filePath)
            throws IOException, IllegalAccessException {
        // 1. 创建工作簿（XSSFWorkbook用于.xlsx格式，HSSFWorkbook用于.xls）
        try (Workbook workbook = new XSSFWorkbook()) {
            // 2. 创建工作表（sheet）
            Sheet sheet = workbook.createSheet("筛单数据");

            // 3. 获取实体类的字段信息（用于生成表头和数据）
            if (dataList == null || dataList.isEmpty()) {
                throw new IllegalArgumentException("导出的数据集合不能为空");
            }
            Class<?> clazz = dataList.get(0).getClass();
            Field[] fields = clazz.getDeclaredFields();

            // 4. 创建表头行（第一行）
            Row headerRow = sheet.createRow(0);
            for (int i = 0; i < fields.length; i++) {
                Field field = fields[i];
                // 设置表头单元格内容（使用字段名作为表头）
                Cell cell = headerRow.createCell(i);
                cell.setCellValue(field.getName());
                // 自动调整列宽
                sheet.autoSizeColumn(i);
            }

            // 5. 填充数据行（从第二行开始）
            for (int rowNum = 0; rowNum < dataList.size(); rowNum++) {
                T data = dataList.get(rowNum);
                Row dataRow = sheet.createRow(rowNum + 1); // 行号从1开始（0是表头）

                for (int colNum = 0; colNum < fields.length; colNum++) {
                    Field field = fields[colNum];
                    field.setAccessible(true); // 允许访问私有字段
                    Object value = field.get(data); // 获取字段值

                    Cell cell = dataRow.createCell(colNum);
                    // 根据字段类型设置单元格值
                    if (value instanceof String) {
                        cell.setCellValue((String) value);
                    } else if (value instanceof Number) {
                        if (value instanceof Integer) {
                            cell.setCellValue((Integer) value);
                        } else if (value instanceof Double) {
                            cell.setCellValue((Double) value);
                        }
                    } else {
                        cell.setCellValue(value != null ? value.toString() : "");
                    }
                }
            }

            // 6. 确保目录存在（若不存在则创建）
            File file = new File(filePath);
            File parentDir = file.getParentFile();
            if (!parentDir.exists()) {
                parentDir.mkdirs(); // 创建多级目录
            }

            // 7. 写入文件
            try (FileOutputStream fos = new FileOutputStream(file)) {
                workbook.write(fos);
            }finally {
                workbook.close();
            }
        }
    }
}
